const crypto = require('crypto');

const passwordIsValid = function(userInput, salt, password) {
  const algorithm = 'aes-192-cbc';
  const iv = Buffer.alloc(16, 1);  // 加密算法向量，16位，全1
  const key = crypto.scryptSync(userInput, salt, 24);
  const encrypted = crypto.createCipheriv(algorithm, key, iv).final('hex');
  return encrypted === password;
};

const removeEmpty = (obj) => {
  Object
    .keys(obj)
    .forEach((key) =>
      (obj[key] == null) && delete obj[key]
    );
  return obj;
};

module.exports = {
  passwordIsValid,
  removeEmpty,
};
